const connection=require('../app/database')

class FileService{
  async createAvatar(mimetype,filename,size,id){
    const statement=`INSERT INTO avatar (mimetype,filename,size,user_id) VALUES (?,?,?,?);`
    const [result]=await connection.execute(statement,[mimetype,filename,size,id])

    return result
  }

  async getAvatarByUserId(userId){
    const statement=`SELECT * FROM avatar WHERE user_id=?;`
    const [result]=await connection.execute(statement,[userId])

    // 返回用户最新上传的头像
    return result.pop()
  }

  async createPicture(mimetype,filename,size,momentId,id){
    const statement=`INSERT INTO file (mimetype,filename,size,moment_id,user_id) VALUES (?,?,?,?,?);`
    const [result]=await connection.execute(statement,[mimetype,filename,size,momentId,id])

    return result
  }

  async getFileByFilename(filename){
    const statement=`SELECT * FROM file WHERE filename=?;`
    const [result]=await connection.execute(statement,[filename])
    
    return result[0]
  }
}

module.exports=new FileService()